package com.controller;

import com.entity.UserEntity;
import com.ex.MyException;
import com.service.UserService;
import com.sun.deploy.net.HttpRequest;
import top.appx.espring.annotation.Action;
import top.appx.espring.annotation.Autowired;
import top.appx.espring.annotation.Controller;
import top.appx.espring.annotation.RequestParam;
import top.appx.espring.vo.ResultVO;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;


@Controller("/User")
public class UserController extends BaseController<UserEntity> {
    @Autowired
    private UserService userService;
    @Action("login")
    public Object login(HttpServletRequest req,@RequestParam("username") String username,@RequestParam("password") String password){
        UserEntity user = userService.login(username,password);
        this.setSessionUser(req.getSession(true), user);
        return ResultVO.success();
    }




    @Action("getLoginUser")
    public Object getLoginUser(HttpServletRequest req){
        UserEntity user = getSessionUserThrowExIfNotExist(req.getSession(true));
        return user;
    }



    @Action("queryPage")
    public Object queryPage(HttpServletRequest request){
        return userService.queryPage(requestQueryPageVO(request));
    }
    @Action("add")
    public void add(UserEntity userEntity){
        userService.add(userEntity);
    }
    @Action("modify")
    public void modify(UserEntity userEntity){
        userService.modify(userEntity);
    }

/*
    @Action("delById")
    public void delById(HttpServletRequest request,@RequestParam("id") int id){
        if(this.getSessionUserThrowExIfNotExist(request.getSession(true)).getId() == id){
            throw new MyException("不能删除自己");
        }
        userService.delById(id);
    }
*/

    @Action("delByIds")
    public void delByIds(HttpServletRequest request){
        userService.delByIds(requestIds(request));
    }

    @Action("resetPasswordByIds")
    public void resetPasswordByIds(HttpServletRequest request){
        String newPassword = request.getParameter("newPassword");
        userService.resetPasswordByIds(requestIds(request),newPassword);
    }
    public void modifyPassword(@RequestParam("username") String username, @RequestParam("oldPassword") String oldPassword,@RequestParam("newPassword") String newPassword){
        userService.modifyPassword(username,oldPassword,newPassword);

    }


    @Action("logout")
    public String logout(HttpServletRequest request){
        setSessionUser(request.getSession(),null);
        return "/page/home/Login.jsp";
    }

}
